package com.watch.service;

import com.zoo.util.CuratorSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * Created by Administrator on 2016/1/22.
 */
@Path("status")
public class StatusService {
    public static final Logger log = LoggerFactory.getLogger(StatusService.class);

//    @GET
//    @Path("createSimpleBean/{a}/{b}/{c}")
//    @Produces( MediaType.APPLICATION_JSON)
//    public TestBean createSimpleBean(@PathParam(value="a")String a, @PathParam(value="b")int b, @PathParam(value="c")long c) {
//        return new TestBean(a, b, c);
//    }

    @GET
    @Path("/local")
    @Produces(MediaType.TEXT_XML)
    public String getLocalStatus(){
        String xml = "<result>local's value is NULL</result>";
        try{
            byte[] pm = CuratorSupport.getLocalAppNodeValue();
            if(pm != null){
                xml = new String(pm);
            }
        }catch (Exception e){
            log.error("获得LOCAL节点状态异常,"+e.getMessage());
        }finally {
            return xml;
        }
    }

    @GET
    @Path("/app")
    @Produces(MediaType.TEXT_XML)
    public String getAppStatus(){
        String xml = "<result>app's value is NULL</result>";
        try{
            byte[] pm = CuratorSupport.getAppNodeValue();
            if(pm != null){
                xml = new String(pm);
            }
        }catch (Exception e){
            log.error("获得APP节点状态异常,"+e.getMessage());
        }finally {
            return xml;
        }
    }

    @GET
    @Path("/changeStatus/{status}")
    @Produces(MediaType.TEXT_XML)
    public String changeStatus(@PathParam(value="status")String status){
        String xml = "<result>ok</result>";
        try{
            if(status != null){
                String temp = status.trim().toUpperCase();
                if(temp.equals("START")){
                    log.error("jetty:启动CHILD进程");
                    CuratorSupport.curator.startProcess(null);
                }else if(temp.equals("KILL")){
                    log.error("jetty:停止CHILD进程");
                    CuratorSupport.curator.killProcess(null);
                }
            }
        }catch (Exception e){
            log.error("设置APP节点状态异常,"+e.getMessage());
            xml = "<result>wrong</result>";
        }finally {
            return xml;
        }
    }

}
